package com.iwop.moto.controller;

import com.iwop.moto.auth.LoginAuth;
import com.iwop.moto.comm.ErrCode;
import com.iwop.moto.model.MOrderPurchase;
import com.iwop.moto.model.ResultModel;
import com.iwop.moto.model.SuperRecordModel;
import com.iwop.moto.service.MOrderPurchaseService;
import com.iwop.moto.service.StoreService;
import com.iwop.moto.utils.PushNoticeUtil;
import com.iwop.moto.utils.stringUtil.SequenceUtil;
import com.iwop.moto.utils.stringUtil.StringUtils;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by mobom on 2016/12/9.
 */

@Api(value = "purchase", description = "采购订单")
@Controller
public class MOrderPurchaseController extends BaseController {
    @Autowired
    StoreService storeService;
    @Autowired
    private MOrderPurchaseService mOrderPurchaseService;

    /**
     * 功能：添加采购订单
     * session_id 用户id
     * detail 订单详情列表 json对象
     */
    @RequestMapping(value = "/shop/purchase/add", method = RequestMethod.POST)
    @ResponseBody
    public ResultModel addOrderPurchase(@ModelAttribute MOrderPurchase mOrderPurchase,
                                        @ApiParam(name = "detail", required = false, value = "订单物品列表", defaultValue = "")
                                        @RequestParam(value = "detail", required = false) String detail,
                                        @ApiParam(name = "session_id", value = "用户id", defaultValue = "")
                                        @RequestParam(value = "session_id") String session_id) {


        String result = null;
        try {
            result = mOrderPurchaseService.insert(mOrderPurchase, detail);
            String buyStoreId = mOrderPurchase.getBuyStoreId();
            SuperRecordModel storeBySid = storeService.getStoreBySid(buyStoreId);
            String user_id = storeBySid.getString("user_id");
            PushNoticeUtil.sendPersonNotice("您有一条新的付款消息", "store", user_id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return getModelByString(result);
    }

    /**
     * 订单状态修改
     *
     * @param session_id 用户id
     * @param oid        订单id
     * @param store_id   买方店铺id
     * @param status     订单状态 订单完成状态0待支付1已支付2拒绝
     */
    @RequestMapping(value = "/consumer/purchase/update", method = RequestMethod.POST)
    @ResponseBody
    public ResultModel updatePurchase(@ApiParam(name = "oid", value = "订单id", defaultValue = "")
                                      @RequestParam(value = "oid") String oid,
                                      @ApiParam(name = "status", value = "订单状态", defaultValue = "")
                                      @RequestParam(value = "status") int status,
                                      @ApiParam(name = "store_id", value = "买方店铺id", defaultValue = "")
                                      @RequestParam(value = "store_id") String store_id,
                                      @ApiParam(name = "session_id", value = "用户id", defaultValue = "")
                                      @RequestParam(value = "session_id") String session_id) {
        String result = mOrderPurchaseService.updateOderPurchaseStatus(oid, status, store_id);
        return getModelByString(result);
    }

    /**
     * 获取在线订单列表
     */
    @RequestMapping(value = "/common/purchase/list", method = RequestMethod.POST)
    @ResponseBody
    @LoginAuth(isval = true)
    public ResultModel getOrderPurchaseList(@ApiParam(name = "seller_store_id", value = "卖家店铺id", required = false, defaultValue = "")
                                            @RequestParam(value = "seller_store_id", required = false) String seller_store_id,
                                            @ApiParam(name = "buy_store_id", value = "买家店铺id", defaultValue = "", required = false)
                                            @RequestParam(value = "buy_store_id", required = false) String buy_store_id) {
        if (StringUtils.isEmpty(seller_store_id) && StringUtils.isEmpty(buy_store_id)) {
            return getErr(ErrCode.ERROR_PARAM);
        } else if (!(StringUtils.isEmpty(seller_store_id)) && !(StringUtils.isEmpty(buy_store_id))) {
            return getErr(ErrCode.ERROR_PARAM);
        }
        List<MOrderPurchase> list = mOrderPurchaseService.findOrderPurchaseList(seller_store_id, buy_store_id);
        return getSucceed(list, ErrCode.SUCCEED);
    }

    /**
     * 获取订单详情
     */
    @RequestMapping(value = "/common/purchase/detail", method = RequestMethod.POST)
    @ResponseBody
    @LoginAuth(isval = true)
    public ResultModel getOrderPurchaseDetail(@ApiParam(name = "oid", value = "订单id", required = false, defaultValue = "")
                                              @RequestParam(value = "oid", required = false) String oid,
                                              @ApiParam(name = "session_id", value = "用户id", defaultValue = "", required = false)
                                              @RequestParam(value = "session_id", required = false) String session_id) {
        try {
            MOrderPurchase mOrderPurchase = mOrderPurchaseService.getPurchaseDetail(oid);
            return getSucceed(mOrderPurchase, ErrCode.SUCCEED);
        } catch (Exception e) {
            e.printStackTrace();
            return getErr(ErrCode.ERROR_SYSTEM);
        }
    }

    /**
     * 功能：获取订单编号
     */
    @RequestMapping(value = "/shop/purchase/get/order_id", method = RequestMethod.GET)
    @ResponseBody
    public ResultModel getOrderId() {
        String co = SequenceUtil.Serial("Co");
        Map<String, String> param = new HashMap<>();
        param.put("order_id", co);
        return getSucceed(param, ErrCode.SUCCEED);
    }
}
